﻿using System;
using System.Collections.Generic;
using DevExpress.XtraEditors;
using EkStudio.Com.UI.Interface;
using EkStudio.com.bean.xml.RootInterface;
using ScintillaNET;
using System.Drawing;
using System.Windows.Forms;
using EkStudio.com.bean.xml.WorkFlow.Node;

namespace EkStudio.com.ui.unitbill
{
    public partial class BillFlowJavaPluginPanel : XtraUserControl, ProjectUnitBillInterface
    {

        private bool readOnly = false;

        private object java = null;

        private ProjectUnitBillInterface parentPage = null;

        private BillFormatModule module = null;

        private ProjectRootPanel RootPanel = null;

        private bool saveStatus = true;

        public BillFlowJavaPluginPanel(BillFormatModule _bean, ProjectRootPanel Root, ProjectUnitBillInterface pRoot, object code,bool Save)
        {
            InitializeComponent();
            module = _bean;
            RootPanel = Root;
            ParentPage = pRoot;
            java = code;
            readOnly = pRoot.ReadOnly;
            saveStatus = Save;
            InitForm();
        }

        private void InitForm()
        {
            #region 初始编辑器
            JavaMemo.Margins[0].Width = 20;
            JavaMemo.Margins[2].Type = MarginType.Symbol;
            JavaMemo.Margins[2].Mask = Marker.MaskFolders;
            JavaMemo.Margins[2].Sensitive = true;
            JavaMemo.Margins[2].Width = 20;

            JavaMemo.Markers[Marker.Folder].Symbol = MarkerSymbol.BoxPlus;
            JavaMemo.Markers[Marker.FolderOpen].Symbol = MarkerSymbol.BoxMinus;
            JavaMemo.Markers[Marker.FolderEnd].Symbol = MarkerSymbol.BoxPlusConnected;
            JavaMemo.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
            JavaMemo.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.BoxMinusConnected;
            JavaMemo.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine;
            JavaMemo.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner;

            JavaMemo.StyleResetDefault();
            JavaMemo.Styles[Style.Default].Font = "Consolas";
            JavaMemo.Styles[Style.Default].Size = 10;
            JavaMemo.StyleClearAll();


            // Configure the CPP (C#) lexer styles
            JavaMemo.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
            JavaMemo.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0); // Green
            JavaMemo.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0); // Green
            JavaMemo.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128); // Gray
            JavaMemo.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
            JavaMemo.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
            JavaMemo.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
            JavaMemo.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JavaMemo.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JavaMemo.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JavaMemo.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
            JavaMemo.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
            JavaMemo.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;
            JavaMemo.Lexer = Lexer.Cpp;

            // Set the keywords
            JavaMemo.SetKeywords(0, "function abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe import virtual while");
            JavaMemo.SetKeywords(1, "boolean var byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void");

            //JavaMemo.SetKeywords(0, common.unit.Label.SQLKeys);
            #endregion

            if(java is WflowEventPluginModule)
            {
                WflowEventPluginModule plugin = java as WflowEventPluginModule;
                this.JavaMemo.Text = plugin.Content;
            }
            else if(java is WflowEventPluginNodeEvent)
            {
                WflowEventPluginNodeEvent plugin = java as WflowEventPluginNodeEvent;
                this.JavaMemo.Text = plugin.Content;
            }
            this.JavaMemo.Enabled = !ParentPage.ReadOnly;

        }

        public string BillKey
        {
            get
            {
                return module.getKey();
            }
        }

        public bool isSubUnitBill
        {
            get
            {
                return true;
            }
        }

        public ProjectUnitBillInterface ParentPage
        {
            get
            {
                return parentPage;
            }

            set
            {
                parentPage = value;
            }
        }

        public bool ReadOnly
        {
            get
            {
                return readOnly;
            }

            set
            {
                readOnly = value;
            }
        }

        public bool SaveStatus
        {
            get
            {
                return saveStatus;
            }

            set
            {
                saveStatus = value;
                this.RootPanel.SaveStatus = value;
            }
        }

        public List<ProjectUnitBillInterface> SubjectPage
        {
            get
            {
                return null;
            }
        }

        public void AddSubjectBill(ProjectUnitBillInterface bill)
        {
            
        }

        public bool close()
        {
            bool result = true;
            if (!saveStatus)
            {
                DialogResult DR = XtraMessageBox.Show("当前数据已更改是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (DR == DialogResult.OK)
                {
                    this.saveData(true);
                }
                else if (DR == DialogResult.Cancel)
                {
                    result = false;
                }
            }
            if (result && parentPage != null)
            {
                parentPage.RemoveSubjectBill(this);
                parentPage = null;
            }
            return result;
        }

        public bool ContainSubject(ProjectUnitBillInterface bill)
        {
            return false;
        }

        public void RemoveSubjectBill(ProjectUnitBillInterface bill)
        {
            
        }

        public void saveData(bool updateParent)
        {
            if (ReadOnly) return;
            if (java is WflowEventPluginModule)
            {
                WflowEventPluginModule plugin = java as WflowEventPluginModule;
                plugin.Content = JavaMemo.Text;
            }
            else if (java is WflowEventPluginNodeEvent)
            {
                WflowEventPluginNodeEvent plugin = java as WflowEventPluginNodeEvent;
                plugin.Content = JavaMemo.Text;
            }
            ParentPage.SaveStatus = false;
            if (updateParent)
            {
                SaveStatus = true;
            }
            else
            {
                saveStatus = true;
            }
        }

        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="clazz">复制后的clazz</param>
        /// <returns></returns>
        public bool Copy(out object clazz)
        {
            bool val = false;
            clazz = null;

            return val;
        }

        private void JavaMemo_CharAdded(object sender, CharAddedEventArgs e)
        {
            SaveStatus = false;
        }

        private void JavaMemo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
            {
                SaveStatus = false;
            }
            else if (e.KeyCode == Keys.V && e.Control && Clipboard.ContainsText())
            {
                SaveStatus = false;
            }
        }

        private void JavaMemo_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
